export function getEventsFromModel(model) {
    if (model) {
        if (typeof model.body === 'string') {
            model.body = JSON.parse(model.body)
        }
        if (model.body) {
            let events = []
            model.body.events.forEach(e => {
                events.push(Object.assign({}, e))
            })
            if (events) {
                if (model.body.rules) {
                    model.body.rules.forEach(rule => {
                        if (rule && rule.content) {
                            rule.content.forEach(c => {
                                if (c.target_id) {
                                    let target = events.find((event) => {
                                        return (event.identifier === c.target_id)
                                    })

                                    if (target) {
                                        target.mexpression = c
                                        target.model_cfg = c.expression
                                    }
                                }
                            })
                        }

                    });
                }
            }
            return events
        }
    }
    return []
}
export function getPropertiesFromModel(model) {
    let properties = []
    if (model) {
        if (typeof model.body === 'string') {
            model.body = JSON.parse(model.body)
        }
        let ps = model.body.properties
        if (ps) {
            ps.forEach(p => {
                let struct = undefined
                if (p.data_type.type === 'struct') {
                    struct = p.data_type.specs
                }
                else if (p.data_type.type === 'array' && p.data_type.specs &&
                    p.data_type.specs.item && p.data_type.specs.item.type === 'struct') {
                        struct = p.data_type.specs.item.specs

                } else {
                    let pro = Object.assign({},p)
                    properties.push(pro)
                    
                }

                if(struct && struct.length > 0){
                    let pro = Object.assign({},p)
                    pro.children = struct
                    properties.push(pro)
                }

            })
        }
    }
    return properties

}
export default {
    getEventsFromModel,
    getPropertiesFromModel,
    //queryThing,
}